﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Utilities;

public partial class en_ucControl_ucRegister : System.Web.UI.UserControl
{
    TblAccountsDAO userDAO = new TblAccountsDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadCountry();
    }
    private void LoadCountry()
    {
        DataSet DSSCountry = new DataSet();
        DSSCountry.ReadXml(Server.MapPath("../App_Data/countries.xml"));
        drcountry.DataSource = DSSCountry;
        drcountry.DataTextField = "name";
        drcountry.DataValueField = "code";
        drcountry.DataBind();
    }
    private bool UserAccount_CheckExists()
    {
        bool bExists = false;
        try
        {
            bExists = userDAO.CheckExits(txtusername.Text, 1);

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
        return bExists;
    }
    
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        bool bFlag = false;

        lblMessage.Text = String.Empty;
        msCaptcha.ValidateCaptcha(txtCaptcha.Text);
        if (!msCaptcha.UserValidated)
        {
            lblMessage.Text = "Verification is Invalid";
            txtCaptcha.Text = String.Empty;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            return;
        }
        if (UserAccount_CheckExists())
        {
            lblMessage.Text = "UserName exists.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            txtCaptcha.Text = String.Empty;
            return;
        }

        try
        {
            EmailService emailSrv = new EmailService();

            TblAccounts user = new TblAccounts();


            user.FirstName = txtFName.Text;
            user.LastName = txtLName.Text;
            user.EmailAcc = txtEmail.Text;
            user.UsernameAcc = txtusername.Text;
            user.PhoneNumber = txtPhone.Text;
            user.TypeAcc = 2;
            user.Display = true;
            user.Country = drcountry.SelectedItem.Text;

            user.PasswordAcc = Encryption.EncryptToString(txtPassword.Text);
         
            userDAO.InsUpdAccounts(user, 1);
            //Send mail     
            user = userDAO.GetAll(user.UsernameAcc);
            string actLink = "http://" + Request.Url.Host + "//en//Default.aspx";

            emailSrv.SendWelcomeMembershipEmail(user, Server.MapPath(Global.MEMBERSHIP_WELCOME), actLink);

            bFlag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            // Response.Redirect(GlobalWeb.ERROR_PAGE, true);
            //ScriptManager.RegisterStartupScript(btnRegister, btnRegister.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
        }
        if (bFlag)
        {
            lblMessage.Text = "Your account has been registered successfully.<br /><br />Registration information has been sent to your email <b><u>" + txtEmail.Text.Trim() + ".";
            //lblMessage.Text = " Bạn đã đăng ký thành công với địa chỉ email <b><u>" + txtEmail.Text.Trim() + "</u></b>. Hãy đăng nhập hệ thống để mua hàng ";
            lblMessage.ForeColor = System.Drawing.Color.Blue;
            pnlRegister.Visible = false;
           // ScriptManager.RegisterStartupScript(btnRegister, btnRegister.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
        }
    }
    protected void lnkRefreshCaptcha_Click(object sender, EventArgs e)
    {
        txtCaptcha.Text = String.Empty;

    }
   
}
